package com.android.project.mysql.connection;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.util.Log;




public class ConnectionData {
	    static Random  rand  = new Random();
	    int idWord;
	    List<AssociationId> listId;
	    List<AssociationWord> listWord;
	    SearchAssociation searchId;
	    SearchWord searchWord;
	    boolean notFound;
	    
	    
	    public  List<AssociationWord> retrieveData(String id){
	    	List<NameValuePair> word = new ArrayList<NameValuePair>();
	    	
	    	
	    	notFound = true;
	    	
	    	while(notFound){
	    		if(!id.equals("-1")){
	    			idWord = Integer.valueOf(id).intValue();
	    		}
	    		else{
	    			
	    			//idWord = rand.nextInt(16780);
	    			//idWord = rand.nextInt(623481);
	    			idWord = rand.nextInt(413215);

	    		}
		    	
		    	Log.d("num",  String.valueOf(idWord));
		    	searchId = new SearchAssociation();
		    	listId = searchId.find(idWord);
		    	
		    	if (listId.size() >= 2){
		    		notFound = false;
		    		Log.d("num",  "maggiore di 2");
		    	}
		    	
	    	
	    	}
	    	
	 
	    	
	    		
	 	
	    	int len = listId.size();
	    	
	    	//int id2 = rand.nextInt(len);
	    	searchWord = new SearchWord();
	    	String ids= String.valueOf(idWord) + ",";
	    	if(len > 5) len = 5;
	    	//word.add(new BasicNameValuePair("id",  String.valueOf(idWord)));
	    	for(int i = 0 ; i < len; i ++){
	    		ids =  ids.concat(listId.get(i).idWord2) + "," ;
	    		//id2 = rand.nextInt(len - 1);
	    		Log.d("connection data ids", ids);
	    		
	    	}
	    	//word.add(new BasicNameValuePair("id1",  String.valueOf(idWord)));
	    	//word.add(new BasicNameValuePair("id2", listId.get(id2).idWord2));
	    	word.add(new BasicNameValuePair("id", ids));
	    	/*if(idWord < Integer.valueOf(listId.get(0).idWord2).intValue()){
	    		listWord = searchWord.find(word,false);
	    	}
	    	else{
	    		listWord = searchWord.find(word,true);
	    	}*/
	    	
	    	listWord = searchWord.find(word);
	    	Log.d("Connection data", " "  + listWord.get(0).word);
	    	Log.d("Connection data", " "  + listWord.get(1).word);
	    	listWord.get(0).setId(String.valueOf(idWord));
	    	
	    	for(int i = 1, j = 0; i < listWord.size(); i++, j++){
	    		listWord.get(i).setId(listId.get(j).idWord2);
	    		
	    	}
    	
	    	return listWord;
	    	
	    }

}
